using System.Web.Security;
using Castle.MonoRail.Framework;
using WineCellar.Web.Controllers;
using WineCellar.Web.Filters;
using WineCellar.Web;

namespace WineCellar.Web.Controllers
{
    [Layout("index")]
    [AuthenticationFilter()]
    public class ProfileController:MessageControllerBase<ProfileController.PropertyAccessorImpl>
    {
        public class PropertyAccessorImpl : PropertyAccessorBase
        {

        }
        public void Index()
        {
            if (IsAjaxRequest) { CancelLayout(); }
        }        
        public void ChangePassword(string oldPassword, string newPassword, string newPasswordVerify)
        {
            if (newPassword != newPasswordVerify)
                this.MessageToAction("Index", "New password does not match with verified password. Please try again.", Message.Enums.MessageClass.Warning);
            else
            {
                RolePrincipal currentUser = SecurityHelper.GetCurrentUser(this.Context);

                if (!Membership.ValidateUser(currentUser.Identity.Name, oldPassword))
                    this.MessageToAction("Index", "Wrong old password. Please try again.", Message.Enums.MessageClass.Warning);
                else
                {
                    MembershipUser user=Membership.GetUser(currentUser.Identity.Name,true);
                    user.ChangePassword(oldPassword, newPassword);
                    this.MessageToAction("Index", "Password changed successfully.",Message.Enums.MessageClass.Information);
                }
            }
        }
        private void MessageToAction(string redirectToAction, string message, Message.Enums.MessageClass messageClass)
        {
            this.PropertyAccessor.AddMessage(new Message(message, messageClass));
            this.RedirectToAction(redirectToAction);
        }
    
    }
}
